﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：IntegratedTestBase.cs
// 功能描述：TongTongMall.TestBase
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using Castle.MicroKernel.Registration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TongTongMall.Collections;
using TongTongMall.Dapper;
using TongTongMall.Dependecy;
using TongTongMall.MallWeb.Application.Configuration.Session;
using TongTongMall.Modules;
using TongTongMall.TestBase.TestBase.Modules;
using TongTongMall.TestBase.TestBase.Session;

namespace TongTongMall.TestBase
{
    public abstract class IntegratedTestBase
    {

        public static bool IsRegister { get; set; } = false;

        protected IIocManager LocalIocManager { get; private set; }



        private readonly TongTongMallBootstrapper _bootstrapper;

        protected IntegratedTestBase()
        {
            LocalIocManager = IocManager.Instance;
            LocalIocManager.RegisterIfNot<IModuleFinder, TestModuleFinder>();
            PreInitialize();
            if (!IsRegister)
            {
                _bootstrapper = new TongTongMallBootstrapper(LocalIocManager);
                _bootstrapper.Initialize();
                IsRegister = true;
            }
        }

        protected virtual void AddModules(ITypeList<BaseModule> modules)
        {
           
        }


        protected virtual void PreInitialize()
        {
            LocalIocManager.RegisterIfNot<IMallWebSession, TestMallWebSession>(DependencyLifeStyle.Singleton);
            
        }

        public virtual void Dispose()
        {
            _bootstrapper.Dispose();
        }

        protected T Resolve<T>()
        {
            EnsureClassRegistered(typeof(T));
            return LocalIocManager.IocContainer.Resolve<T>();
        }


        protected T Resolve<T>(object argumentsAsAnonymousType)
        {
            EnsureClassRegistered(typeof(T));
            return LocalIocManager.IocContainer.Resolve<T>(argumentsAsAnonymousType);
        }


        protected object Resolve(Type type)
        {
            EnsureClassRegistered(type);
            return LocalIocManager.IocContainer.Resolve(type);
        }


        protected object Resolve(Type type, object argumentsAsAnonymousType)
        {
            EnsureClassRegistered(type);
            return LocalIocManager.IocContainer.Resolve(type, argumentsAsAnonymousType);
        }


        protected void EnsureClassRegistered(Type type, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Transient)
        {
            if (!LocalIocManager.IsRegistered(type))
            {
                if (!type.IsClass || type.IsAbstract)
                {
                    throw new Exception(type.Name + "不能注册.");
                }

                LocalIocManager.Register(type, lifeStyle);
            }
        }
    }
}
